package com.mycompany.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.rpc.*;
import com.google.gwt.core.client.*;
import com.google.gwt.user.client.*;
import com.google.gwt.json.client.*;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class gwttest implements EntryPoint
{

	final Label label = new Label();
	final Label label2 = new Label();
	final Label label3 = new Label();
	final Label label4 = new Label();

	TextBox t1 = new TextBox();
	TextBox t2 = new TextBox();


	/**
   * This is the entry point method.
	 */
	public void onModuleLoad()
	{
		final Button button = new Button("Click me");
		final Button button2 = new Button("Click me");
		final Button button4 = new Button("Get session var");
		final Button button3 = new Button("Clear");
		final Button button5 = new Button("Do List");

		button.addClickListener(new ClickListener()
		{
			public void onClick(Widget sender)
			{
				handleClickOne();
			}
		});

		button2.addClickListener(new ClickListener()
		{
			public void onClick(Widget sender)
			{
				handleClickFour();
			}
		});

		button4.addClickListener(new ClickListener()
		{
			public void onClick(Widget sender)
			{
				handleClickFive();
			}
		});

		button3.addClickListener(new ClickListener()
		{
			public void onClick(Widget sender)
			{
				label.setText("");
				label2.setText("");
			}
		});

		button5.addClickListener(new ClickListener()
		{
			public void onClick(Widget sender)
			{
				handleClickSix();
			}
		});

		// Assume that the host HTML has elements defined whose
		// IDs are "slot1", "slot2".  In a real app, you probably would not want
		// to hard-code IDs.  Instead, you could, for example, search for all 
		// elements with a particular CSS class and replace them with widgets.
		//
		RootPanel.get("slot1").add(button);
		RootPanel.get("slot2").add(label);

		RootPanel.get("slot3").add(button2);
		RootPanel.get("slot4").add(label2);

		RootPanel.get("slot5").add(button3);

		RootPanel.get("slot6").add(t1);
		RootPanel.get("slot7").add(t2);

		RootPanel.get("slot8").add(button4);
		RootPanel.get("slot9").add(label3);

		RootPanel.get("slot10").add(button5);
		RootPanel.get("slot11").add(label4);
	}

	private void handleClickThree()
	{
		label.setText("Searching ...");
		if (!HTTPRequest.asyncGet("http://localhost:3454/MyJSonService.ashx?tosearch=",
			new JSONResponseTextHandler()))
		{
			label.setText("Search");
		}
	}

	private void handleClickTwo()
	{
		XmlRpcClient client = new XmlRpcClient("http://localhost:3454/MyCallService.ashx");
		Object[] params = new Object[2];
		params[0] = new Integer(5);
		params[1] = "str5";
		AsyncCallback callback = new AsyncCallback()
		{
			public void onSuccess(Object result)
			{
				label.setText(result.toString());
			}

			public void onFailure(Throwable caught)
			{
				label.setText(caught.toString());
			}
		};
		client.execute("method", params, callback);
	}

	private void handleClickOne()
	{
		MyCallServiceAsync myService = (MyCallServiceAsync)GWT.create(MyCallService.class);
		ServiceDefTarget endpoint = (ServiceDefTarget)myService;
		//String moduleRelativeURL = GWT.getModuleBaseURL() + "email";
		//String moduleRelativeURL = "http://localhost:3893/GwtService.ashx";
		//String moduleRelativeURL = "http://localhost:3454/GwtService.ashx";
		String moduleRelativeURL = GWT.getModuleBaseURL() + "MyCallService";
		endpoint.setServiceEntryPoint(moduleRelativeURL);

		AsyncCallback callback = new AsyncCallback()
		{
			public void onSuccess(Object result)
			{
				//label.setText(((Person)result).getName());
				label.setText(result.toString());
			}

			public void onFailure(Throwable caught)
			{
				label.setText(caught.toString());
			}
		};

		//myService.doCalc(9999999, 111111111, callback);
		//String[] str = new String[] {"Mark", "Anneleen", "Beckers"} ;
		//myService.doCalcTwo(str, 200.14, 15, 'M', callback);
		Person p = new Person("Mark", "jefke2212@gmail.com", 24);
		/*p.setName("Mark");
		p.setEmail("jefke2212@gmail.com");
		p.setAge(24);*/
		label.setText(p.getName());
		myService.doPerson(p, 22.5, callback);

	}

	private void handleClickFour()
	{
		TestServiceAsync myService = (TestServiceAsync)GWT.create(TestService.class);
		ServiceDefTarget endpoint = (ServiceDefTarget)myService;
		String moduleRelativeURL = "http://localhost:3893/GwtService.ashx";
		endpoint.setServiceEntryPoint(moduleRelativeURL);

		AsyncCallback callback = new AsyncCallback()
		{
			public void onSuccess(Object result)
			{
				label2.setText(result.toString());
			}

			public void onFailure(Throwable caught)
			{
				label2.setText(caught.toString());
			}
		};

		int one = Integer.parseInt(t1.getText());
		int two = Integer.parseInt(t2.getText());

		myService.doMath(one, two, callback);
	}

	private void handleClickFive()
	{
		TestServiceAsync myService = (TestServiceAsync)GWT.create(TestService.class);
		ServiceDefTarget endpoint = (ServiceDefTarget)myService;
		String moduleRelativeURL = "http://localhost:3893/GwtService.ashx";
		endpoint.setServiceEntryPoint(moduleRelativeURL);

		AsyncCallback callback = new AsyncCallback()
		{
			public void onSuccess(Object result)
			{
				label3.setText(result.toString());
			}

			public void onFailure(Throwable caught)
			{
				label3.setText(caught.toString());
			}
		};

		myService.getSessionVar(callback);
	}

	private void handleClickSix()
	{
		TestServiceAsync myService = (TestServiceAsync)GWT.create(TestService.class);
		ServiceDefTarget endpoint = (ServiceDefTarget)myService;
		String moduleRelativeURL = "http://localhost:3893/GwtService.ashx";
		endpoint.setServiceEntryPoint(moduleRelativeURL);

		AsyncCallback callback = new AsyncCallback()
		{
			public void onSuccess(Object result)
			{
				label4.setText(result.toString());
			}

			public void onFailure(Throwable caught)
			{
				label4.setText(caught.toString());
			}
		};

		/**
		* This field is a Set that must always contain Strings.
		* 
		* @gwt.typeArgs <java.lang.String>
		*/
		java.util.List list = new java.util.ArrayList() ;
		list.add(new Person("Mark","mark@ville.be",25));
		list.add(new Person("Jeentje", "jeentje@ville.be", 23));

		myService.doList(list, callback);
	}

	private class JSONResponseTextHandler implements ResponseTextHandler
	{
		/*
		 *  (non-Javadoc)
		 * @see com.google.gwt.user.client.ResponseTextHandler#onCompletion(java.lang.String)
		 */

		public void onCompletion(String responseText)
		{
			/*
		   *  When the fetch has been completed we parse the JSON response and
		   *  display the result
		   */

			/*JSONObject jsonObject;
			try
			{
				jsonObject = JSONParser.parse(responseText);

			}
			catch (JSONException e)
			{

			}*/

			label.setText(responseText);

		}
	}
}
